<template>
  <!--  class="mt-6" -->
  <div style="display: flex; justify-content: space-between; align-items: center; padding: 0 24px"
    ><span style="font-weight: 550">按钮操作</span
    ><a-button type="primary" class="ml-2" @click="addAuthHandle">添加</a-button></div
  >
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              disabled: record.dataModelId !== '-2',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              disabled: record.dataModelId !== '-2',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <ActionAuthModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts" name="ActionAuths">
  import { defineComponent, watch, onMounted } from 'vue';
  import { actionAuths } from '../post.data';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import ActionAuthModal from './ActionAuthModal.vue';
  import { delUsinghandledefAuthApi } from '/@/api/admin/auth';
  import { resetModelList } from '../post.data';

  export default defineComponent({
    name: 'ActionAuth',
    components: { BasicTable, TableAction, ActionAuthModal },
    props: ['data', 'moduleId', 'isModelBind'],
    emits: ['success'],
    setup(props, { emit }) {
      const { createMessage } = useMessage();
      const [registerModal, { openModal }] = useModal();

      const [registerTable, { setTableData }] = useTable({
        columns: actionAuths,
        pagination: false,
        isTreeTable: true,
        striped: false,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });

      watch(
        () => props.data,
        (v) => {
          v && setTableData(v);
        },
      );

      onMounted(() => {
        setTableData(props.data);
      });
      function handleEdit(record: Recordable) {
        resetModelList();
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        console.log('record', record);
        const { id } = record;
        await delUsinghandledefAuthApi(id);
        createMessage.success('操作成功');
        handleSuccess();
      }

      function addAuthHandle() {
        openModal(true, {
          moduleId: props.moduleId,
          isUpdate: false,
        });
      }

      function handleSuccess() {
        emit('success');
      }

      return {
        registerTable,
        handleEdit,
        handleDelete,
        addAuthHandle,
        registerModal,
        handleSuccess,
      };
    },
  });
</script>
<style lang="less" scoped></style>
